package com.sk.weichat.ui.circle;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.R;
import com.sk.weichat.bean.SubjectItemBean;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.util.ToastUtil;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.ListCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ArrayResult;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * @author lzs
 * @date 2019/10/30
 * Email:343067508@qq.com
 **/
public class TopiclistActivity extends BaseActivity {

    List<SubjectItemBean> data = new ArrayList<>();
    BaseQuickAdapter<SubjectItemBean, BaseViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_list);
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvTitle = findViewById(R.id.tv_title_center);
        tvTitle.setText(getString(R.string.dynamic_square_system_topic));
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new BaseQuickAdapter<SubjectItemBean, BaseViewHolder>(R.layout.item_topics, data) {

            @Override
            protected void convert(BaseViewHolder helper, SubjectItemBean item) {
                helper.setText(R.id.tv_count, item.getMsgSum() + "条动态");
                TextView tvName = helper.getView(R.id.tv_name);
                tvName.setText(item.getTopicName());
            }
        };
        recyclerView.setAdapter(adapter);
        loadTopicData();
    }

    /**
     * 获取话题数据
     */
    private void loadTopicData() {
        HashMap<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus(mContext).accessToken);
        HttpUtils.post().url(CoreManager.getConfig(mContext).QUERY_SYS_TOPIC)
                .params(params)
                .build()
                .execute(new ListCallback<SubjectItemBean>(SubjectItemBean.class) {
                    @Override
                    public void onResponse(ArrayResult<SubjectItemBean> result) {
                        if (result.getResultCode() == 1) {
                            List<SubjectItemBean> datas = result.getData();
                            data.addAll(datas);
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtil.showToast(mContext, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {

                        ToastUtil.showToast(mContext, e.getMessage());
                    }
                });
    }

}
